package com.hyt.it.ogt.controller.ykcj.exam;

import cn.hutool.core.util.StrUtil;
import com.hyt.it.ogt.common.config.ApiVersionConstant;
import com.hyt.it.ogt.controller.base.BaseController;
import com.hyt.it.ogt.ykcj.common.core.domain.AjaxResult;
import com.hyt.it.ogt.ykcj.common.core.page.TableDataInfo;
import com.hyt.it.ogt.ykcj.common.enums.BizErrorCode;
import com.hyt.it.ogt.ykcj.common.exception.CustomException;
import com.hyt.it.ogt.ykcj.domain.param.ExamMergeParam;
import com.hyt.it.ogt.ykcj.domain.param.ExamNameQueryParam;
import com.hyt.it.ogt.ykcj.domain.param.ExamPageParam;
import com.hyt.it.ogt.ykcj.domain.vo.EnterpriseExamVo;
import com.hyt.it.ogt.ykcj.domain.vo.ExamConfigNameVo;
import com.hyt.it.ogt.ykcj.service.exam.IExamInfoService;
import com.hyt.swagger.ApiVersion;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 考试信息Controller
 *
 * @author admin
 * @date 2021-06-18
 */
@Api(tags = "exam.1.考试信息", value = "考试信息请求接口")
@ApiSort(value = 101)
@RestController
@RequestMapping("/exam/info")
public class ExamInfoController extends BaseController {
    @Autowired
    private IExamInfoService examInfoService;

    @ApiOperation("获取考试详情")
    @GetMapping("/detail/{id}")
    public AjaxResult examDetail(@PathVariable("id") String id) {
        return AjaxResult.success(examInfoService.examDetail(id));
    }


    @ApiOperation("获取考试形式")
    @GetMapping("/getExamForms")
    public AjaxResult getExamForms() {
        return AjaxResult.success(examInfoService.getExamForms());
    }

    /**
     * 获取考试信息详细信息
     *
     * @throws Exception
     */
    @ApiOperation("获取考试信息详细信息")
    @ApiImplicitParam(name = "id", value = "考试ID", dataType = "String", required = true)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) throws Exception {
        if (StringUtils.isEmpty(id)) {
            return AjaxResult.error(BizErrorCode.EXAM_E0002.getErrMsg());
        }
        return AjaxResult.success(examInfoService.selectExamInfoById(id));
    }

    @ApiOperation("查询考试组下的所有考试")
    @GetMapping("/getSubExamList/{examId}")
    public AjaxResult getSubExamList(@PathVariable("examId") String examId) {
        return AjaxResult.success(examInfoService.getSubExamList(examId));
    }

    @ApiOperation("合并考试")
    @PostMapping("/merge")
    public AjaxResult examMerge(@RequestBody ExamMergeParam examMergeParam) {
        examInfoService.examMerge(examMergeParam);
        return AjaxResult.success();
    }

    @ApiOperation("根据考试查询合格和区间")
    @GetMapping("/getExamQualifiedStatus")
    public AjaxResult getExamQualifiedStatus(@RequestParam(value = "examId", required = false) String examId, @RequestParam(value = "subjectId", required = false) String subjectId) {
        return AjaxResult.success(examInfoService.getExamQualifiedStatus(examId, subjectId));
    }

    @ApiOperation(value = "查询备选栏考试列表", response = EnterpriseExamVo.class)
    @PostMapping("/select/list")
    public TableDataInfo selectList(@RequestBody ExamPageParam examPageParam) {
        startPage();
        List<EnterpriseExamVo> list = examInfoService.pageEnterpriseExamInfoSelectList(examPageParam);
        TableDataInfo dataTable = getDataTable(list);
        dataTable.setTotal(examPageParam.getTotal() == null ? 0L : examPageParam.getTotal());
        return dataTable;
    }

    @ApiOperation(value = "查询已选栏考试列表", response = EnterpriseExamVo.class)
    @PostMapping("/selectedst")
    public AjaxResult selectedList(String examId) {
        return AjaxResult.success(examInfoService.pageEnterpriseExamInfoSelectedList(examId));
    }

    @ApiOperation(value = "合并考试明细", response = EnterpriseExamVo.class)
    @PostMapping("/mergeExamDetail")
    public AjaxResult mergeExamDetail(String examId) {
        return AjaxResult.success(examInfoService.mergeExamDetail(examId));
    }

    @ApiOperation("根据考试类型查询考试名称")
    @ApiImplicitParam(name = "examType", value = "考试类型（0:专业技术;1:公务员;2:企业招聘;99:其他考试 ）", dataType = "String")
    @PostMapping("/getExamNameList")
    public AjaxResult getExamNameList(@RequestBody ExamNameQueryParam examNameQueryParam) {
        String officeId = getOfficeId();
        Set<String> deptIds = getDeptIds();
        examNameQueryParam.setOfficeId(officeId);
        examNameQueryParam.setDeptIds(deptIds);
        List<ExamConfigNameVo> list = examInfoService.getExamNameList(examNameQueryParam);
        return AjaxResult.success(list);
    }

    @ApiOperation("根据考试获取考试绑定的证书列表")
    @ApiVersion(group = ApiVersionConstant.V2_4_9)
    @PostMapping("/getCertificateList")
    public AjaxResult getCertificateList(@ApiParam("考试ID") String examId) {
        if (StrUtil.isBlank(examId)) {
            throw new CustomException("当前无可查询的考试成绩，请去考试管理创建考试");
        }
        return AjaxResult.success(examInfoService.getCertificateList(examId));
    }


    @ApiOperation("解除关系")
    @GetMapping("/relationRemove/{id}")
    public AjaxResult relationRemove(@PathVariable String id) {
        examInfoService.relationRemove(id);
        return AjaxResult.success();
    }
}


